package com.alibaba.cola.demo.controller;

import com.alibaba.cola.demo.common.Result;
import com.alibaba.cola.demo.domain.Product;
import com.alibaba.cola.demo.dto.ProductDto;
import com.alibaba.cola.demo.dto.ProductQry;
import com.alibaba.cola.demo.service.ProductService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RequestMapping
@RestController
public class ProductController {

    @Autowired
    private ProductService productService;

    @GetMapping("/all")
    public Result<List<Product>> all() {
        List<Product> listAll = productService.listAll();
        return Result.SUCCESS(listAll);
    }

    @GetMapping("/products")
    public Result<List<ProductDto>> listProduct(@RequestParam("name") String name) {
        ProductQry productQry = new ProductQry();
        productQry.setName(name);
        Result<List<ProductDto>> response = productService.findByQry(productQry);
        return response;
    }
}
